package com.neusoft.util;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@Component
@Aspect
@EnableAspectJAutoProxy
public class LogAOP {
	private final static Logger log = LoggerFactory.getLogger(LogAOP.class);

	// 切点方法
	@Pointcut("execution(* com.neusoft.controller.*.*(..))")
	private void watch() {

	}

	// 前置通知方法
	@Before(value = "watch()")
	public void before(JoinPoint jp) {
		// 获得方法签名
		Signature sig = jp.getSignature();
		// 获得业务方法名称
		String methodName = sig.getName();
		// 获得业务方法所在的控制器类的方法
		String className = sig.getDeclaringTypeName();
		log.info("业务方法执行前，获取方法信息");
		log.info("类名：{},方法名：{}", className, methodName);

		// 获取访问本服务端应用的客户端的url和ip
		ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		HttpServletRequest request = sra.getRequest();
		String url=request.getRequestURL().toString();
		String ip=request.getRemoteAddr();
		log.info("客户端请求的地址是：{},客户端的ip是：{}", url, ip);
	}
	
	//异常抛出通知方法
	@AfterThrowing(value="watch()",throwing="ex")
	public void delException(JoinPoint jp,Exception ex){
		log.debug("异常信息：",ex);
	}
}
